<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!--返回错误信息-->
<?php echo validation_errors(); ?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php
    $title = [
        'title' => '供应商管理',
    ];
    $this->load->view('/admin/common/header', $title);
    ?>
</head>

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>
<div class="am-g tpl-g">

<!--    头部   侧边菜单-->
    <?php $this->load->view('/admin/common/bodyLeft'); ?>
    <script type="text/javascript">
    	$(".js-sidebar-nav>li:nth-child(5)>a").addClass("active");
    </script>
    <!-- 内容区域 -->
    <div class="tpl-content-wrapper">
        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <form action="/admin/supplier/remove" id="form_admin" method="post">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <div class="widget-title  am-cf">供应商管理</div>
                        </div>
                            <div class="widget-body  am-fr">
                                <div class="am-u-sm-12 am-u-md-6 am-u-lg-6 am-form-group">
                                    <div class="am-btn-toolbar">
                                        <div class="am-alert am-alert-danger" id="data-alert" style="display: none;margin: 4px 0 10px 10px;font-size: 1.6rem;">
                                            <p>开始日期应小于结束日期</p>
                                        </div>
                                        <div class="am-g">
                                            <div class="am-u-sm-6">
                                                <button type="button" class="am-btn am-btn-default am-margin-right" id="my-start">开始日期</button><span id="my-startDate"></span>
                                            </div>
                                            <div class="am-u-sm-6">
                                                <button type="button" class="am-btn am-btn-default am-margin-right" id="my-end">结束日期</button><span id="my-endDate"></span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">

                                    <div class=" am-u-md-3 ">
                                        <div class="am-form-group tpl-table-list-select">
                                            <select data-am-selected="{btnSize: 'sm',btnWidth: '100%'}" id="size" class="choose">
                                                <option value=""></option>
                                                <option value="0-99人">0-99人</option>
                                                <option value="100-499人">100-499人</option>
                                                <option value="500-999人">500-999人</option>
                                                <option value="1000-9999人">1000-9999人</option>
                                                <option value="10000人以上">10000人以上</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class=" am-u-md-6 " id="distpicker">
                                        <div class="am-form-group tpl-table-list-select">
                                            <select data-am-selected="{btnSize: 'sm',btnWidth: '32%',maxHeight: 200}" id="province" class="choose"></select>
                                            <select data-am-selected="{btnSize: 'sm',btnWidth: '32%',maxHeight: 200}" id="city" class="choose"></select>
                                            <select data-am-selected="{btnSize: 'sm',btnWidth: '32%',maxHeight: 200}" id="district" class="choose"></select>
                                        </div>
                                    </div>
                                    <div class=" am-u-md-3">
                                        <div class="am-form-group am-input-group am-input-group-sm tpl-form-border-form cl-p">
        <!--                                    <form action="/admin/user/search" method="get">-->
                                            <input type="text" class="am-form-field" name="search" id="search" placeholder="请输入供应商名称">
                                            <span class="am-input-group-btn">
                                                <a href="javascript:;" id="searchBtn" class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search">搜索</a>
                                            </span>
        <!--                                    </form>-->
                                        </div>
                                    </div>
                                    
                                </div>
                                    <div class="am-u-sm-12">
                                        <div class="am-btn-toolbar am-u-sm-12 am-form-group">
                                            <div class=" am-btn-group-xs am-fl">
                                                <a href="/admin/supplier/addView" class="am-btn am-btn-default am-btn-primary"><i class="am-icon-plus"></i> 新增供应商</a>
                                                <a href="/admin/supplier/daochu" class="am-btn am-btn-default am-btn-primary"><i class="am-icon-download"></i> 导出</a>
                                            </div>
                                            <div class="am-btn-group-xs am-fr">
                                                <a href="javascript:;" class="am-btn am-btn-default am-btn-danger all-delete"><i class="am-icon-trash"></i> 批量删除</a>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="am-u-sm-12">
                                        <table  class="am-table am-table-compact am-table-striped tpl-table-black " >
                                            <thead>
                                            <tr>
                                                <th style="width: 70px"><input type="checkbox" class="checkall" id="checkallBottom">全选</th>
                                                <th style="width: 60px">序号</th>
                                                <th style="width: 110px">创建日期</th>
                                                <th style="width: 120px">供应商编码</th>
                                                <th style="width: 140px">供应商名称</th>
                                                <th style="width: 100px">供应商类型</th>
                                                <th style="width: 80px">报价系数</th>
                                                <th style="width: 80px">联系人</th>
                                                <th style="width: 100px">联系电话</th>
                                                <th>地址</th>
                                                <th style="width: 140px">操作</th>
                                            </tr>
                                            </thead>
                                            <tbody class="choose-box">
                                            <?php
    //                                        print_r($list);
    //                                        die();
                                            if(isset($list) && $list){
                                                foreach ($list as $v) {
                                                    echo '<tr class="gradeX">
                                                    <td>
                                                        <label class="am-checkbox-inline">
                                                          <input type="checkbox" value='.$v['id'].' name="del_id[]" class="checkitem">
                                                        </label>
                                                     </td>';
                                                    echo '<td>'.$v['id'].'</td>
                                                    <td>'.date('Y-m-d',$v['createTime']).'</td>
                                                     <td><a href="/admin/supplier/editView?id='.$v['id'].'">'.$v['code'].'</a></td>
                                                   <td>'.$v['name'].'</td>
                                                   <td>'.$v['type'].'</td>
                                                   <td class="offerNum">'.$v['offerNum'].'</td>
                                                   <td>'.$v['linkMan'].'</td>
                                                   <td>'.$v['phone'].'</td>
                                                   <td>'.$v['address'].'</td>
                                                    <td>
                                                        <div class="tpl-table-black-operation">
                                                         <a href="javascript:;" data-am-modal="{target: \'#grade-box\', closeViaDimmer: 0, width: 400, height: 250}" class="click-grade"><i class="am-icon-paint-brush" ></i> 去评分 </a>
                                                         <a href="javascript:;" data-id="'.$v['id'].'" class="tpl-table-black-operation-del">
                                                            <i class="am-icon-trash"></i> 删除
                                                        </a>
                                                        </div>
                                                    </td>
                                            </tr>';
                                                }

                                            }
                                            ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <div class="am-u-lg-12">
                                        <div class="am-u-sm-centered">
                                            <ul class="am-pagination am-pagination-centered" id="paging">

                                            </ul>
                                        </div>
                                    </div>
                                </div>
                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>
</div>
<!-- 模态框 -->
<div class="am-modal am-modal-no-btn" tabindex="-1" id="grade-box">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">评分
            <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
        </div>
        <div class="am-modal-bd">
            <form class="am-form am-form-horizontal">
                <div class="am-form-group">
                    <label for="basic" class="am-u-sm-4 am-form-label">基本情况分：</label>
                    <div class="am-u-sm-8">
                        <input type="text" id="basic" placeholder="输入基本情况分">
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="appoint" class="am-u-sm-4 am-form-label">履约情况分：</label>
                    <div class="am-u-sm-8">
                        <input type="text" id="appoint" placeholder="输入履约情况分">
                    </div>
                </div>
            </form>
            <div style="position: relative;top: 25px;">
                <button type="button" class="am-btn am-btn-primary am-u-sm-6" id="confirm" data-am-modal-close>确定</button>
                <button type="button" class="am-btn am-btn-danger am-u-sm-6" data-am-modal-close>取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 模拟confirm -->
<div class="am-modal am-modal-confirm" tabindex="-1" id="my-confirm">
  <div class="am-modal-dialog">
    <div class="am-modal-bd">
      确定要删除记录吗？
    </div>
    <div class="am-modal-footer">
      <span class="am-modal-btn" data-am-modal-cancel>取消</span>
      <span class="am-modal-btn" data-am-modal-confirm>确定</span>
    </div>
  </div>
</div>

<!-- alert -->
<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
  <div class="am-modal-dialog">
    <div class="am-modal-hd">提示</div>
    <div class="am-modal-bd">
      请选择删除项！
    </div>
    <div class="am-modal-footer">
      <span class="am-modal-btn">确定</span>
    </div>
  </div>
</div>

<script src="/assets/js/amazeui.min.js"></script>
<script src="/assets/js/amazeui.datatables.min.js"></script>
<script src="/assets/js/dataTables.responsive.min.js"></script>
<script src="/assets/js/distpicker.data.js"></script>
<script src="/assets/js/distpicker.js"></script>
<script src="/assets/js/app.js"></script>
<script src="/assets/js/page.js"></script>
<script>


    //批量删除
    $(".all-delete").on("click",function(){
        if(!$(".checkitem:checked").length){
            $("#my-alert").modal();
            return;
        }
        $('#my-confirm').modal({
            onConfirm: function(options) {
                $('#form_admin').submit();
            },
            onCancel: function() {
            }
        });
    });
    //单个删除
    $(document).on("click",".tpl-table-black-operation-del",function(){
        var thisId = $(this).data("id");
        $('#my-confirm').modal({
            onConfirm: function(options) {
                window.location.href = "/admin/supplier/remove?id=" + thisId;
            },
            onCancel: function() {
            }
        });
    });

    /* 全选 反选*/
    $('.checkall').click(function(){
        $(".checkitem").prop("checked",this.checked);
    });
    var gradeX,id;
    //点击去评分
    $(document).on("click",".click-grade",function(){
        gradeX = $(this).parents(".gradeX");
        id = gradeX.find(".checkitem").val();
    });
    // 点击确定提交评分
    $("#confirm").on("click",function(){
        var basic = $("#basic");
        var appoint = $("#appoint");
        var reg = new RegExp("^(\\d|[1-9]\\d|100)$");
        if(!reg.test(basic.val()) || !reg.test(appoint.val())){
            alert("请输入0-100评分。");
            return false;
        }else{
            $.ajax({
                url:"/admin/supplier/score",
                data:{
                    base:basic.val(),//基本评分
                    perform:appoint.val(), //履约情况分
                    id: id//要更新的id
                },
                dataType:"json",
                type:"post",
                async:false,
                success:function(data){
                   var offerNum = data.success;
                   gradeX.find(".offerNum").text(offerNum);
                   basic.val("");
                   appoint.val("");
                },
                error:function(error){
                    alert(error);
                }
            });         
        }
    });

    var startTime = "";     //开始时间
    var endTime = "";       //结束时间
    var size = "";          //规模
    var province = "";      //省
    var city = "";          //县
    var district = "";      //区
    var search = "";        //搜索
    var page = 1;           //页数

    $("#searchBtn").on("click",function(){
        startTime = $("#my-startDate").text();  
        endTime = $("#my-endDate").text();
        size = $("#size").val();
        province = $("#province").val();
        city = $("#city").val();
        district = $("#district").val();
        search = $("#search").val();

        chooseSupplier(startTime,endTime,size,province,city,district,search,page);

    });

    //筛选
    $(".choose").change(function(){
        startTime = $("#my-startDate").text();  
        endTime = $("#my-endDate").text();
        size = $("#size").val();
        province = $("#province").val();
        city = $("#city").val();
        district = $("#district").val();
        search = $("#search").val();

        chooseSupplier(startTime,endTime,size,province,city,district,search,page);

    });

    //筛选日期
    var startDate = new Date(2014, 11, 20);
    var endDate = new Date(2100, 11, 25);
    var $alert = $('#data-alert');
    $('#my-start').datepicker().on('changeDate.datepicker.amui', function(event) {

        if (event.date.valueOf() > endDate.valueOf()) {
          $alert.find('p').text('开始日期应小于结束日期！').end().show();
          return false;
          
        } else {
          $alert.hide();
          startDate = new Date(event.date);
          $('#my-startDate').text($('#my-start').data('date'));
        }
        $(this).datepicker('close');
        startTime = $("#my-startDate").text();  
        endTime = $("#my-endDate").text();
        size = $("#size").val();
        province = $("#province").val();
        city = $("#city").val();
        district = $("#district").val();
        search = $("#search").val();
        
        chooseSupplier(startTime,endTime,size,province,city,district,search,page);
        
    });
    $('#my-end').datepicker().on('changeDate.datepicker.amui', function(event) {

        if (event.date.valueOf() < startDate.valueOf()) {
          $alert.find('p').text('结束日期应大于开始日期！').end().show();
          return false;
        } else {
          $alert.hide();
          endDate = new Date(event.date);
          $('#my-endDate').text($('#my-end').data('date'));
        }
        $(this).datepicker('close');

        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        size = $("#size").val();
        province = $("#province").val();
        city = $("#city").val();
        district = $("#district").val();
        search = $("#search").val();
        
        chooseSupplier(startTime,endTime,size,province,city,district,search,page);
        
    });

    function chooseSupplier(startTime,endTime,size,province,city,district,search,page){
        $.ajax({
            url:"/api/supplier/search",
            data:{
                startTime: startTime,
                endTime: endTime,
                size: size,
                province: province,
                city: city,
                district: district,
                search: search,
                page: page
            },
            dataType:"json",
            type:"GET",
            beforeSend:function(){
                $(".my-dark").css("display","block");
            },
            success:function(msg){
                var data = msg['0'];
                var list = "";
                var offNum = "";
                if(!data[0]){
                    list += '<tr class="gradeX"><td colspan="11">暂无数据</td></tr>'
                }else{
                    for(var i = 0;i < data.length; i++){
                        if(data[i].offerNum){
                            offNum = data[i].offerNum;
                        }else{
                            offNum = "";
                        }
                        
                        list += '<tr class="gradeX">'
                                +'<td>'
                                    + '<label class="am-checkbox-inline">'
                                        + '<input type="checkbox" value="'+ data[i].id +'" name="del_id[]" class="checkitem">'
                                    + '</label>'
                                + '</td>'
                                + '<td>'+ data[i].id +'</td>'
                                + '<td>'+ data[i].createTime +'</td>'
                                + '<td><a href="/admin/supplier/editView?id='+ data[i].id +'">'+ data[i].code +'</a></td>'
                                + '<td>'+ data[i].name +'</td>'
                                + '<td>'+ data[i].type +'</td>'
                                + '<td class="offerNum">'+ offNum +'</td>'
                                + '<td>'+ data[i].linkMan +'</td>'
                                + '<td>'+ data[i].phone +'</td>'
                                + '<td>'+ data[i].address +'</td>'
                                + '<td>'
                                    + '<div class="tpl-table-black-operation">'
                                        + '<a href="javascript:;" data-am-modal="{target: \'#grade-box\', closeViaDimmer: 0, width: 400, height: 250}" class="click-grade"><i class="am-icon-paint-brush" ></i> 去评分 </a> '
                                        + '<a href="javascript:;" data-id="'+ data[i].id +'" class="tpl-table-black-operation-del">'
                                            + '<i class="am-icon-trash"></i> 删除'
                                        + '</a>'
                                    + '</div>'
                                + '</td>'
                            + '</tr>'
                    }
                }
                $(".choose-box").html(list);

                //分页
                //设置分页信息  
                var pageOptions = {  
                    AllowPaging: true,  
                    PageIndex: msg.this_page,      //设置当前页码  
                    PageSize: msg.pageNum,       //设置分页大小  
                    RecordCount: msg.total_num,  //设置数据总数  
                    // TotalPage: ,      //设置总页数
                    showPageCount: 4,  
                    onPageClick: function(pageIndex) {  
                        chooseSupplier(startTime,endTime,size,province,city,district,search,pageIndex);

                    }  
                }  
                //初始化分页栏  
                $('#paging').pageBar(pageOptions); 


            },
            complete:function(){
                $(".my-dark").css("display","none");
            },
            error:function(data){
                alert(data);
            }
        });
    }



    
</script>

<script>
    $(document).ready(function() {  
        $('#distpicker').distpicker({
            autoSelect: false
        });
        //设置分页信息  
        var pageOptions = {  
            AllowPaging: true,  
            PageIndex: <?php echo $_GET['page'] ?>,      //设置当前页码  
            PageSize: <?php echo $pageNum ?>,       //设置分页大小  
            RecordCount: <?php echo $totalNum ?>,  //设置数据总数  
            // TotalPage: ,      //设置总页数
            showPageCount: 4,  
            onPageClick: function(pageIndex) {  
                
                var page = parseInt(pageIndex);

                window.location.href = "?page="+ page; 
                
            }  
        }  
        //初始化分页栏  
        $('#paging').pageBar(pageOptions);      
    })  

</script>
</body>

</html>